/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.incremental;

import java.awt.Color;
import java.awt.Font;
import java.util.AbstractList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Observer;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.UIManager;
import syntax.Clock;
import syntax.ClockConditionAtom;
import syntax.ClockGuard;
import syntax.Location;
import syntax.Message;
import syntax.Operator;
import syntax.Operator.Op;
import syntax.Repository;
import syntax.Transition;

/**
 *
 * @author ennouaary
 */
public class JClockGuardDialogue extends javax.swing.JDialog {

    private ClockConditionAtom conditionAtom;
    private int l_b, u_b;
    private Op l_op = null, u_op = null;
    //private Transition transition;
    private LinkedList<Clock> clockList = new LinkedList<Clock>();
    boolean newNotEdit = true;
    private TransitionObservable observable;
    private Collection<Clock> clocks;
    private ClockGuard clockGuard;
    

//    public Transition getTransition() {
//        return transition;
//    }
//
//    public void setTransition(Transition transition) {
//        this.transition = transition;
//    }
        
    /**
     * Creates new form JClockGuardDialogue
     */
    public JClockGuardDialogue(TransitionObservable observable, 
            Collection<Clock> clocks, ClockGuard clockGuard) {
        this.clocks = clocks;        
        this.clockGuard = clockGuard;
        initComponents();
        newNotEdit = true;
        //this.transition = ((JTransitionFrame)(observable.getFrame())).getTransition();
        conditionAtom = new ClockConditionAtom();
        this.observable = observable;
        fillInComponents();
    }

    public JClockGuardDialogue(ClockConditionAtom conditionAtom, TransitionObservable observable,
            Collection<Clock> clocks, ClockGuard clockGuard) {
        this.clocks = clocks;
        this.clockGuard = clockGuard;
        initComponents();
        newNotEdit = false;
        //this.transition = ((JTransitionFrame)(observable.getFrame())).getTransition();
        this.conditionAtom = conditionAtom;
        this.observable = observable;
        fillInComponents();
    }

    private void fillInComboOperator(JComboBox combo) {
        combo.removeAllItems();
        combo.addItem(Operator.Op.EQ);
        combo.addItem(Operator.Op.LE);
        combo.addItem(Operator.Op.LT);
    }

    private void fillInOperators() {
        fillInComboOperator(jLRComboBox);
        fillInComboOperator(jURComboBox);
        jLRComboBox.setSelectedItem(conditionAtom.getLowerRelation());
        jURComboBox.setSelectedItem(conditionAtom.getUpperRelation());
    }

    private void fillInClocksNewAtom() {
//        Repository repository = Repository.getInstance();
        jClockComboBox.removeAllItems();
        clockList.removeAll(null);
//        for (Clock clock : repository.getAllClocks()) {
        for (Clock clock : clocks) {
            if (!clockGuard.containsClock(clock)) {
                clockList.add(clock);
                jClockComboBox.addItem(clock);
            }
        }
    }

    private void fillInClocksEditAtom() {
        jClockComboBox.removeAllItems();
        clockList.removeAll(null);
        clockList.add(this.conditionAtom.getClock());
        jClockComboBox.addItem(this.conditionAtom.getClock());
    }

    private void fillInComponentsNew() {
        fillInClocksNewAtom();
        jLowerBoundSlider.setValue(0);
        jUpperBoundSlider.setValue((int) Math.floor(parameters.Parameters.getInfinity()));
    }

    private void fillInComponentsEdit() {
        fillInClocksEditAtom();
        //jClockVariableLabel.setText(conditionAtom.getClock().getName());
        jLowerBoundSlider.setValue((int) conditionAtom.getDomain().getL_b());
        jUpperBoundSlider.setValue((int) conditionAtom.getDomain().getU_b());
    }

    private void fillInComponents() {
        int infinity = (int) Math.floor(parameters.Parameters.getInfinity());
        jLowerBoundSlider.setMinimum(0);
        jLowerBoundSlider.setMaximum(infinity);
        jUpperBoundSlider.setMinimum(0);
        jUpperBoundSlider.setMaximum(infinity);
        UIManager.put("ComboBox.disabledForeground", Color.black);
        jClockComboBox.setEnabled(newNotEdit);
        
        //jClockComboBox.setVisible(newNotEdit);

        if (newNotEdit) {
            fillInComponentsNew();
        } else {
            fillInComponentsEdit();
        }
        fillInOperators();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel3 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLowerBoundSlider = new javax.swing.JSlider();
        jUpperBoundSlider = new javax.swing.JSlider();
        jLowerBoundDisplayValueLabel = new javax.swing.JLabel();
        jUpperBoundDisplayValueLabel = new javax.swing.JLabel();
        jURComboBox = new javax.swing.JComboBox();
        jLRComboBox = new javax.swing.JComboBox();
        jClockComboBox = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jOkButton = new javax.swing.JButton();
        jCancelButton = new javax.swing.JButton();
        jResetButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Clock Guard Atom Modification");
        setModal(true);
        getContentPane().setLayout(new java.awt.FlowLayout());

        jPanel3.setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(30, 0, 0, 0);
        jPanel3.add(jLabel9, gridBagConstraints);

        jPanel1.setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(30, 0, 0, 0);
        jPanel1.add(jLabel4, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 0, 0);
        jPanel1.add(jLabel5, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 0, 0);
        jPanel1.add(jLabel6, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 10;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 0, 0);
        jPanel1.add(jLabel7, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 12;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 0, 0);
        jPanel1.add(jLabel8, gridBagConstraints);

        jLabel1.setText("Clock Variable");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        jPanel1.add(jLabel1, gridBagConstraints);

        jLabel2.setText("Lower Bound");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
        jPanel1.add(jLabel2, gridBagConstraints);

        jLabel3.setText("Upper Bound");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 13;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        jPanel1.add(jLabel3, gridBagConstraints);

        jLowerBoundSlider.setMajorTickSpacing(10);
        jLowerBoundSlider.setMinorTickSpacing(1);
        jLowerBoundSlider.setOrientation(javax.swing.JSlider.VERTICAL);
        jLowerBoundSlider.setPaintLabels(true);
        jLowerBoundSlider.setPaintTicks(true);
        jLowerBoundSlider.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jLowerBoundSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jLowerBoundSliderStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipady = 164;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        jPanel1.add(jLowerBoundSlider, gridBagConstraints);

        jUpperBoundSlider.setMajorTickSpacing(10);
        jUpperBoundSlider.setMinorTickSpacing(1);
        jUpperBoundSlider.setOrientation(javax.swing.JSlider.VERTICAL);
        jUpperBoundSlider.setPaintLabels(true);
        jUpperBoundSlider.setPaintTicks(true);
        jUpperBoundSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jUpperBoundSliderStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 13;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipady = 164;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(jUpperBoundSlider, gridBagConstraints);

        jLowerBoundDisplayValueLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLowerBoundDisplayValueLabel.setForeground(new java.awt.Color(255, 0, 0));
        jLowerBoundDisplayValueLabel.setText("---");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        jPanel1.add(jLowerBoundDisplayValueLabel, gridBagConstraints);

        jUpperBoundDisplayValueLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jUpperBoundDisplayValueLabel.setForeground(new java.awt.Color(255, 0, 0));
        jUpperBoundDisplayValueLabel.setText("---");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 11;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(jUpperBoundDisplayValueLabel, gridBagConstraints);

        jURComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "---" }));
        jURComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jURComboBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 9;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(jURComboBox, gridBagConstraints);

        jLRComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "---" }));
        jLRComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jLRComboBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 7;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        jPanel1.add(jLRComboBox, gridBagConstraints);

        jClockComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "---" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_START;
        jPanel1.add(jClockComboBox, gridBagConstraints);

        jPanel3.add(jPanel1, new java.awt.GridBagConstraints());

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jOkButton.setText("OK");
        jOkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jOkButtonActionPerformed(evt);
            }
        });
        jPanel2.add(jOkButton);

        jCancelButton.setText("Cancel");
        jCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelButtonActionPerformed(evt);
            }
        });
        jPanel2.add(jCancelButton);

        jResetButton.setText("Reset");
        jResetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jResetButtonActionPerformed(evt);
            }
        });
        jPanel2.add(jResetButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        jPanel3.add(jPanel2, gridBagConstraints);

        getContentPane().add(jPanel3);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jOkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jOkButtonActionPerformed
        // TODO add your handling code here:
        this.conditionAtom.getDomain().setL_b(jLowerBoundSlider.getValue());
        this.conditionAtom.getDomain().setU_b(jUpperBoundSlider.getValue());
        this.conditionAtom.setClock((Clock)jClockComboBox.getSelectedItem());
        if (newNotEdit) {
            clockGuard.addClockConditionAtom(conditionAtom);
        }
        observable.notifyTransitionChange();
        dispose();        
    }//GEN-LAST:event_jOkButtonActionPerformed

    private void reset() {
        l_b = (int) getConditionAtom().getDomain().getL_b();
        jLowerBoundSlider.setValue(l_b);
        u_b = (int) getConditionAtom().getDomain().getU_b();
        jUpperBoundSlider.setValue(u_b);
        u_op = getConditionAtom().getUpperRelation();
        jURComboBox.setSelectedItem(u_op);
        l_op = getConditionAtom().getUpperRelation();
        jLRComboBox.setSelectedItem(l_op);
    }

    private void jLowerBoundSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jLowerBoundSliderStateChanged
        // TODO add your handling code here:
        l_b = jLowerBoundSlider.getValue();
        u_b = jUpperBoundSlider.getValue();
        jLowerBoundDisplayValueLabel.setText(Integer.toString(jLowerBoundSlider.getValue()));
        if (l_op == null || u_op == null) {
            return;
        }
        if ((l_b > u_b)
                || (l_op.equals(Op.EQ) && u_op.equals(Op.EQ) && l_b != u_b)) {
            reset();
        }

        jLowerBoundDisplayValueLabel.setText(Integer.toString(jLowerBoundSlider.getValue()));
    }//GEN-LAST:event_jLowerBoundSliderStateChanged

    private void jUpperBoundSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jUpperBoundSliderStateChanged
        // TODO add your handling code here:
        l_b = jLowerBoundSlider.getValue();
        u_b = jUpperBoundSlider.getValue();
        jUpperBoundDisplayValueLabel.setText(Integer.toString(jUpperBoundSlider.getValue()));
        if (l_op == null || u_op == null) {
            return;
        }
        if ((l_b > u_b)
                || (l_op.equals(Op.EQ) && u_op.equals(Op.EQ) && l_b != u_b)) {
            reset();
        }
        jUpperBoundDisplayValueLabel.setText(Integer.toString(jUpperBoundSlider.getValue()));
    }//GEN-LAST:event_jUpperBoundSliderStateChanged

    private void jURComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jURComboBoxActionPerformed
        // TODO add your handling code here:
        if (jURComboBox.getSelectedItem() == null) {
            return;
        }
        l_op = Operator.parseOperator(jLRComboBox.getSelectedItem().toString());
        u_op = Operator.parseOperator(jURComboBox.getSelectedItem().toString());
        if (u_op.equals(Operator.Op.EQ)
                && (l_op.equals(Operator.Op.EQ))
                && (l_b != u_b)) {
            reset();
        }
    }//GEN-LAST:event_jURComboBoxActionPerformed

    private void jLRComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jLRComboBoxActionPerformed
        // TODO add your handling code here:
        if (jLRComboBox.getSelectedItem() == null) {
            return;
        }
        l_op = Operator.parseOperator(jLRComboBox.getSelectedItem().toString());
        u_op = Operator.parseOperator(jURComboBox.getSelectedItem().toString());
        if (u_op.equals(Operator.Op.EQ)
                && (l_op.equals(Operator.Op.EQ))
                && (l_b != u_b)) {
            reset();
        }
    }//GEN-LAST:event_jLRComboBoxActionPerformed

    private void jResetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jResetButtonActionPerformed
        // TODO add your handling code here:
        reset();
    }//GEN-LAST:event_jResetButtonActionPerformed

    private void jCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelButtonActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jCancelButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JClockGuardDialogue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JClockGuardDialogue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JClockGuardDialogue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JClockGuardDialogue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new JClockGuardDialogue().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jCancelButton;
    private javax.swing.JComboBox jClockComboBox;
    private javax.swing.JComboBox jLRComboBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLowerBoundDisplayValueLabel;
    private javax.swing.JSlider jLowerBoundSlider;
    private javax.swing.JButton jOkButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JButton jResetButton;
    private javax.swing.JComboBox jURComboBox;
    private javax.swing.JLabel jUpperBoundDisplayValueLabel;
    private javax.swing.JSlider jUpperBoundSlider;
    // End of variables declaration//GEN-END:variables

    public ClockConditionAtom getConditionAtom() {
        return conditionAtom;
    }

    public void setConditionAtom(ClockConditionAtom conditionAtom) {
        this.conditionAtom = conditionAtom;
    }
}
